﻿
$(document).ready(function () {
    $(".calendar-date").datepicker();
});

// First: Create config
ko.validation.configure({
	registerExtenders: true,
	messagesOnModified: true,
	insertMessages: true,
	parseInputAttributes: true,
	messageTemplate: null,
	decorateElement: true,
	errorClass: "error",
	errorMessageClass: "error-desc"
});

// Second: Create new model to apply to view
var viewModel = new function () {
    // About you
    this.Title = ko.observable().extend({ maxLength: 250 }).extend({ required: { message: "Tiêu đề tài liệu là bắt buộc"} });
    this.LongDesc = ko.observable();
    this.ImagePath = ko.observable();
    this.ImageName = ko.observable();
    this.OriginalAttachName = ko.observable();
    this.AttachName = ko.observable();
    this.AttachType = ko.observable();
    this.IsPublic = ko.observable(true);

    // Categories
    this.AllCategories = ko.observable(false);
    this.CategoryList = ko.observableArray(data.ListCategory);
    this.CategoryID = ko.observable($.cookie("member_categoryId") != null ? $.cookie("member_categoryId") : data.ListCategory[0].ID);
    this.SubCategoryOptionList = ko.observableArray(data.ListSubCategory);
    this.SubCategoryList = ko.computed(function () {
        var cate = this.CategoryID();
        return ko.utils.arrayFilter(this.SubCategoryOptionList(), function (option) {
            return option.ParentID == cate;
        });
    }, this);
    this.SubSelectedID = ko.computed(function () {
        var subCatCookie = $.cookie("member_subcategoryId") != null ? $.cookie("member_subcategoryId") : 0;
        var subCatID = this.SubCategoryList()[0].ID;
        if (subCatCookie != 0)
            return subCatCookie;
        else
            return subCatID;
    }, this);
    //Language
    this.LanguageList = ko.observableArray(data.ListLanguage);
    //DocmentType
    this.DocumentTypeList = ko.observableArray(data.ListDocumentType);

    // Submit, Next button
    this.InvalidateForm = ko.observable(false);
    this.SubmitForm = function () {
        if (viewModel.errors().length == 0) {
            // set cookie
            $.cookie("member_categoryId", $('#CategoryID').val(), { path: '/', expires: 365 });
            $.cookie("member_subcategoryId", $('#SubCategoryID').val(), { path: '/', expires: 365 });
            $.cookie("member_stateId", $('#StateID').val(), { path: '/', expires: 365 });
            $.cookie("member_districtId", $('#DistrictID').val(), { path: '/', expires: 365 });

            // Submit form
            $('form').submit();
        }
        else {
            viewModel.errors.showAllMessages();
            this.InvalidateForm(true);
        }
    }
};

viewModel.errors = ko.validation.group(viewModel);

// Third: Apply view model to view to handle action when user input data
ko.validation.rules.pattern.message = 'Invalid.';
ko.applyBindings(viewModel, document.getElementById("main"));

$(document).ready(function () {
    // Refresh dropdown
    var tempEl = document.getElementById("SubCategoryID");
    if (typeof tempEl.jcf != "undefined" && tempEl.jcf) tempEl.jcf.destroy();
    jcf.customForms.replaceElement("SubCategoryID");
    setTimeout("jcf.customForms.replaceElement('SubCategoryID')", 0);

    $('#CategoryID').change(function () {
        var tempEl = document.getElementById("SubCategoryID");
        if (typeof tempEl.jcf != "undefined" && tempEl.jcf) tempEl.jcf.destroy();
        jcf.customForms.replaceElement("SubCategoryID");
        setTimeout("jcf.customForms.replaceElement('SubCategoryID')", 0);
    });
});